#include <iostream>
#include <string>
#include <stdexcept>
#include "SDL.h"
#include "SDL_ttf.h"
#include "sdltext.h"
using namespace std;

SDLText::SDLText(int x, int y, int r, int g, int b, int size, std::string font, std::string text) throw(sdltext_error)
{
       font_ = NULL;
       txt_surface_ = NULL;

       // Set the color of the text.
       color_ = new SDL_Color;
       color_->r = r;
       color_->g = g;
       color_->b = b;

       // Add default path to font
       font = "adventure2/graphics/fonts/" + font;

       // Open the font
       font_ = TTF_OpenFont(font.c_str(), size);
       if(font_ == NULL)
       {
	      throw sdltext_error("Could not load " + font);
       }

       // Render text with font to surface
       txt_surface_ = TTF_RenderText_Solid(font_, text.c_str(), *color_);
       if(txt_surface_ == NULL)
       {
	      throw sdltext_error("Could not render text to surface");
       }

       // Set location and size.
       dst_rect_ = new SDL_Rect;
       dst_rect_->x = x;
       dst_rect_->y = y;
       dst_rect_->w = txt_surface_->w;
       dst_rect_->h = txt_surface_->h;
}
SDLText::~SDLText()
{
       SDL_FreeSurface(txt_surface_);
       delete color_;
       delete dst_rect_;
       TTF_CloseFont(font_);
}

SDLText::SDLText(int r, int g, int b, int size, std::string font, std::string text) throw(sdltext_error)
{
       font_ = NULL;
       txt_surface_ = NULL;

       // Set the color of the text.
       color_ = new SDL_Color;
       color_->r = r;
       color_->g = g;
       color_->b = b;

       // Add default path to font
       font = "adventure2/graphics/fonts/" + font;

       // Open the font
       font_ = TTF_OpenFont(font.c_str(), size);
       if(font_ == NULL)
       {
	      throw sdltext_error("Could not load " + font);
       }

       // Render text with font to surface
       txt_surface_ = TTF_RenderText_Solid(font_, text.c_str(), *color_);
       if(txt_surface_ == NULL)
       {
	      throw sdltext_error("Could not render text to surface");
       }

       // Set location and size.
       dst_rect_ = new SDL_Rect;
       dst_rect_->x = 0;
       dst_rect_->y = 0;
       dst_rect_->w = txt_surface_->w;
       dst_rect_->h = txt_surface_->h;
}

void SDLText::set_text(std::string new_text) throw(sdltext_error)
{
       SDL_FreeSurface(txt_surface_);
       txt_surface_ = NULL;
       txt_surface_ = TTF_RenderText_Solid(font_, new_text.c_str(), *color_);
       if(txt_surface_ == NULL)
       {
	      throw sdltext_error("Could not render text to surface");
       }

       dst_rect_->w = txt_surface_->w;
       dst_rect_->h = txt_surface_->h;
}

void SDLText::set_pos(int x, int y)
{
       dst_rect_->x = x;
       dst_rect_->y = y;
}

int SDLText::get_width() const
{
       return txt_surface_->w;
}

int SDLText::get_height() const
{
       return txt_surface_->h;
}
void SDLText::render(struct SDL_Surface *dst_)
{
       SDL_BlitSurface(txt_surface_, NULL, dst_, *&dst_rect_);
}
